package com.zlp.partice.io.biochat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Scanner;

/**
 * @author: zhulaiping
 * @time: 2022/8/8 16:48
 */
public class BioDemo2_Chat_client {

    public static void main(String[] args) throws IOException {
        InetAddress byName = InetAddress.getByName("127.0.0.1");
        Socket socket = new Socket(byName,9999);
        PrintStream ps = new PrintStream(socket.getOutputStream());
        Scanner sc = new Scanner(System.in);
        new Lister(socket).start();
        while (true){
            System.out.println("请输入待发送消息：");
            String msg = sc.nextLine();
            if(msg.equals("11")) break;
            ps.println(msg);
            ps.flush();
        }


        socket.shutdownOutput();
        socket.close();
    }

    static class Lister extends Thread{
        private Socket socket;
        public Lister(Socket socket){
            this.socket = socket;
        }

        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String msg;
                //PrintStream ps = new PrintStream(socket.getOutputStream());
                while ((msg = br.readLine()) != null){
                    System.out.println("收到了信息："+msg);;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
